package com.li.onjava8.concurrent.pl;

import java.util.Arrays;

/**
 * @author li
 * @date 2023/10/30 10:13
 */
public class Summing2 {
    static long basicSum(long[] arr) {
        long sum = 0;
        int size = arr.length;
        for (int i = 0; i < size; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static final int SZ = 20_000_000;
    public static final long CHECK = (long)SZ * ((long)SZ + 1)/2;

    public static void main(String[] args) {
        long[] arr = new long[SZ+1];
        Arrays.parallelSetAll(arr, i -> i);
        Summing.timeTest("Array Basic Sum", CHECK, () -> basicSum(arr));
        Summing.timeTest("Array Sum Stream", CHECK, () -> Arrays.stream(arr).sum());
        Summing.timeTest("Array Sum Stream Parallel", CHECK, () -> Arrays.stream(arr).parallel().sum());
        Summing.timeTest("Array Sum Stream parallelPrefix", CHECK, () -> {
            Arrays.parallelPrefix(arr, Long::sum);
            return arr[arr.length - 1];
        });
    }
}
